import threading

from PyQt5 import QtCore
from PyQt5.QtWidgets import QVBoxLayout, QPushButton, QMessageBox

from common.yaml_config import config
from logs.log import get_log
from ui.BaseWin import BaseWin

log = get_log("MainWin")


class MainWin(BaseWin):
    switch_win = QtCore.pyqtSignal(object)

    def __init__(self, app):
        super(MainWin, self).__init__(app)
        self.init_ui()

    def init_ui(self):
        projects = config.get_projects()

        vbox = QVBoxLayout()

        for i in range(len(projects)):
            pro = projects[i]
            btn = QPushButton(pro['title'], self)
            btn.setCheckable(True)  # 1
            btn.setObjectName(str(i))
            btn.toggled.connect(self.btn_click)
            vbox.addWidget(btn, 2)
        vbox.addStretch(1)

        # 将网格布局应用到当前窗口
        self.setLayout(vbox)
        self.setGeometry(400, 400, 400, 400)
        self.set_center()
        self.setWindowTitle("机器人")
        self.show()

    def btn_click(self, checked):
        log.info("打印测试开始")
        btn = self.sender()
        index = int(self.sender().objectName())
        pro = config.get_project(index)
        log.info(str(index))
        self.switch_win.emit(pro)
        self.close()
        # def my_thread():
        #     try:
        #         print(pro)
        #         eval(pro['eval'])
        #     except object as e:  # 未捕获到异常，程序直接报错
        #         print(e)
        #         QMessageBox.critical(self, "提示", str(e))
        #     finally:
        #         btn.setEnabled(True)
        #
        # btn.setEnabled(False)
        # thread = threading.Thread(target=my_thread)
        # thread.start()

    # def print_btn_click(self):
    #     print("打印测试开始")
    #
    #     def open():
    #         driver = None
    #         try:
    #             driver = EdgeDriverConfig().driver_config()
    #             page = PrintLoginPage()
    #             page.to_login(driver, "https://59.255.67.8/#", "111111")
    #             page.to_print(driver)
    #             time.sleep(2)
    #         except object as e:  # 未捕获到异常，程序直接报错
    #             print(e)
    #         finally:
    #             self.print_btn.setEnabled(True)
    #             try:
    #                 driver.close()
    #             except Exception as e1:
    #                 print(e1)
    #
    #     self.print_btn.setEnabled(False)
    #     thread = threading.Thread(target=open)
    #     thread.start()
    #
    # def bx_btn_click(self):
    #     print("show child")
    #
    #     def open():
    #         driver = None
    #         try:
    #             driver = EdgeDriverConfig().driver_config()
    #             LoginPage().to_login(driver, "username")
    #             time.sleep(0.2)
    #             home_page = HomePage()
    #             home_page.click_menu(driver, "报销管理")
    #             time.sleep(0.2)
    #             home_page.click_menu(driver, "我要报销")
    #             time.sleep(0.1)
    #             home_page.to_normal_bx(driver)
    #             time.sleep(2)
    #         except object as e:  # 未捕获到异常，程序直接报错
    #             print(e)
    #         finally:
    #             self.bx_btn.setEnabled(True)
    #             try:
    #                 driver.close()
    #             except Exception as e1:
    #                 print(e1)
    #
    #     self.bx_btn.setEnabled(False)
    #     thread = threading.Thread(target=open)
    #     thread.start()
